/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lzmpt.imchat.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.hyphenate.easeui.utils.StatusBarUtil;
import com.lzmpt.imchat.DemoApplication;
import com.lzmpt.imchat.R;
import com.lzmpt.imchat.conference.CallFloatWindow;
import com.lzmpt.imchat.conference.ConferenceActivity;
import com.hyphenate.easeui.ui.EaseBaseActivity;
import com.lzmpt.imchat.conference.ConferenceInviteActivity;

import java.util.List;


public class BaseActivity extends EaseBaseActivity {

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        if (!isDialog()) {
            setTheme(R.style.AppTheme);
        }

        initStatus();
        //根据状态栏颜色来决定状态栏文字用黑色还是白色
        StatusBarUtil.setStatusBarMode(this, true, R.color.btn_white_normal);
        initStatus();

    }

    public boolean isDialog() {
        return false;
    }

    public void initStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            } else {
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }


    }

    @Override
    protected void onResume() {
        super.onResume();
        checkIfConferenceExit();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * 将此方法放置在基类，用于检查是否有正在进行的音视频会议
     */
    private void checkIfConferenceExit() {
        // 如果当前的activity是否是ConferenceActivity
        if (this instanceof ConferenceActivity || this instanceof ConferenceInviteActivity) {
            return;
        }
        UserActivityLifecycleCallbacks lifecycleCallbacks = DemoApplication.getInstance().getLifecycleCallbacks();
        if (lifecycleCallbacks == null) {
            return;
        }
        List<Activity> activityList = lifecycleCallbacks.getActivityList();
        if (activityList != null && activityList.size() > 0) {
            for (Activity activity : activityList) {
                if (activity instanceof ConferenceActivity) {
                    //如果没有显示悬浮框，则启动ConferenceActivity
                    if (activity.isFinishing()) {
                        return;
                    }
                    if (!CallFloatWindow.getInstance(DemoApplication.getInstance().applicationContext).isShowing()) {
                        ConferenceActivity.startConferenceCall(this, null);
                    }
                }
            }
        }
    }

}
